package madixin.t977;

// 滑动窗口
class Solution {
    public int[] sortedSquares(int[] nums) {
        if (nums.length ==  0){
            return nums;
        }
        int res[] = new int[nums.length];
        int left = 0;
        int right = nums.length - 1;
        int p = nums.length - 1;
        while (left <= right){
            int squareLeft = nums[left] * nums[left];
            int squareRight = nums[right] * nums[right];
            if (squareLeft >= squareRight){
                res[p] = squareLeft;
                p--;
                left++;
            }else{
                res[p] = squareRight;
                p--;
                right--;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        s.sortedSquares(new int[]{-4,-1,0,3,1});
    }
}